--luAPL definitions

ubuntulibname = "./libluAPL.so"; macosxlibname = "luAPL"
mingwlibname = "libluAPL.dll"; msvclibname = "luAPL.dll"
initfunc = "luaopen_luapl"; macosxinitfunc = "_luaopen_luapl"

regluapl = package.loadlib(mingwlibname, initfunc)
if (not regluapl) then regluapl = package.loadlib(msvclibname, initfunc) end
if (not regluapl) then regluapl = package.loadlib(ubuntulibname, initfunc) end
if (not regluapl) then regluapl = package.loadlib(macosxlibname, macosxinitfunc) end

--print debug info
--print(package.loadlib(msvclibname, initfunc))
--io.read()

res = pcall(regluapl,nil)
if (res) then print("luAPL loaded") else print("Error loading luAPL") end

luAPL = {}
luAPL.__index = luAPL
    function luAPL:assign(val)
      luapl.assigninto(val,assigninto,unpack(self.args))
      return val
    end
    function luAPL:debug()
      luapl.debug()
    end

function A(...)
  local obj = luapl.assign(unpack(arg))
  return obj
end

function S(...)
  local obj = luapl.symbol(unpack(arg))
  return obj
end

function LV(...)
  -- record arguments for a left value
  return setmetatable({args = {unpack(arg)}},luAPL)
end

function AA(...)
  -- create a tuple
  -- AA(1,2,3) == A( A(1), A(2), A(3) )
  obj = luapl.strand(unpack(arg))
  return obj
end

------------------------------------------------------------------------------
-- luAPL standard library
------------------------------------------------------------------------------

function issf(x)
-- return true if x is a slotfiller
  return luapl.issf(x)
end

function alsf(...)
-- convert an association list (sym,val, sym,val, ...) to a slotfiller
  return luapl.alsf(unpack(arg))
end

function flat(x)
-- flattens a nested array of uniform type to a tuple
  return luapl.flat(x)
end

function uniq(x)
  -- return the unique elements of x
  bv = A( A(x,find,x), equals, A(til,count,x) )
  return A( bv, compress, x)
end
